package com.ruoyi.framework.config;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

@Configuration
@PropertySource(value = { "classpath:application-druid.yml" })
public class UfloConfig  {

	@Autowired
    private Environment environment;

    @Bean("ufloSessionFactory")
    public LocalSessionFactoryBean localSessionFactoryBean() throws
            PropertyVetoException, IOException {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan("com.bstek.uflo.model*");
        Properties prop = new Properties();
        prop.put("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
        prop.put("hibernate.show_sql",true);
        prop.put("hibernate.hbm2ddl.auto","update");
        prop.put("hibernate.jdbc.batch_size",100);
        sessionFactoryBean.setHibernateProperties(prop);

        return sessionFactoryBean;
    }
    
    // 数据源配置
    @Bean("ufloDataSource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getProperty("spring.datasource.driverClassName"));
        dataSource.setUrl(environment.getProperty("spring.datasource.druid.master.url"));
        dataSource.setUsername(environment.getProperty("spring.datasource.druid.master.username"));
        dataSource.setPassword(environment.getProperty("spring.datasource.druid.master.password"));
        return dataSource;
    }
    
}